using System.Collections.Generic;
using UnityEngine;

namespace NL.Advanced
{

    public enum ObjState : uint
    {
        None = 0,
        MoveNo = 1,
        AttackNo = 2,
        SkillNo = 4,

        CombatNo = 6,
        ActionNo = 7,
    }
    public interface IObjStater
    {
        ObjState State { get; }
        GameObject gameObject { get; }
    }

    public interface IObjStateOwner
    {
        uint State { get; }

        bool StateWith(ObjState state);

        void StateAdd(IObjStater stater);
        void StateRemove(IObjStater stater);

        void StateClean();
    }




    public partial class CombatObj<P> : IObjStateOwner
    {



        uint IObjStateOwner.State
        {
            get
            {
                uint state = 0;
                foreach (var stater in AllStaters)
                {
                    state |= (uint)stater.State;
                }
                return state;
            }
        }

        protected List<IObjStater> AllStaters = new List<IObjStater>();

        public bool StateWith(ObjState state)
        {
            foreach (var stater in AllStaters)
            {
                if (((uint)stater.State & (uint)state) == (uint)state)
                    return true;

            }
            return false;
        }
        Dictionary<ObjState, GameObject> _StateEffects=new Dictionary<ObjState, GameObject>();


        public void StateAdd(IObjStater stater)
        {

            AllStaters.Add(stater);
            try
            {
                if (!_StateEffects.ContainsKey(stater.State))
                {
                    var mod = CombatSetting_UnitState.GetEFF(stater.State);
                    if (mod != null)
                    {
                        var eff = BPoolMan.POOL["EFF_US"].Create(mod, transform);

                        _StateEffects.Add(stater.State, eff);
                    }
                }
            }catch
            {

            }

        }
        public void StateRemove(IObjStater stater)
        {
            AllStaters.Remove(stater);
            try
            {
                if (!StateWith(stater.State) && _StateEffects.ContainsKey(stater.State))
                {
                    _StateEffects[stater.State]?.SetActive(false);
                }
            }
            catch
            { 
            
            }

        }

        public void StateClean()
        {
            AllStaters.Clear();
            try
            {
                foreach (var a in _StateEffects.Values)
                {
                    if (a != null) a.SetActive(false);
                }
            }finally
            {
                _StateEffects.Clear();
            }


        }
    }
}

